home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / VideoToolbox 97.08.16 / VideoToolboxSources / Identify.c < prev    next >
Encoding:
Text File  |  1997-07-09  |  32.3 KB  |  973 lines  |  [TEXT/CWIE]

  1. /* Apple's NameRegistryLib is included on the Metrowerks CodeWarrior CD-ROM. */
  2. //#define NO_NAME_REGISTRY_LIB 1    /* Uncomment this if you're missing NameRegistryLib */
  3. /*
  4. Identify.c
  5.  
  6. NOTE: several routines that use Apple's NameRegistryLib appear at the end of this file.
  7. If you don't have that file, then un-comment the first line above.
  8.  
  9. Each routine returns an informative C string. Here are samples. (I suggest you
  10. use BreakLines() to printf the longer ones.)
  11.  
  12. IdentifyCompiler:
  13. "Compiled by Metrowerks CodeWarrior C 7.00 for PowerPC, 4B int and 8B double."
  14.  
  15. IdentifyOwner:
  16. "Denis Pelli"
  17.  
  18. IdentifyModel:
  19. "PowerBook 170"
  20.  
  21. IdentifyMachine:
  22. "Denis Pelli's PowerBook 170, 68030, 68882, System 7.1.0."
  23. "Mal's PowerMac 8500/120, ppc604, L2 cache, System 7.5.2."
  24.  
  25. IdentifyVideo(device):
  26. "PowerBook 170 \"Macintosh D Built-In Video\" (.Display_Video_Apple_TIM) in slot 0"
  27. "PowerMac 8500/120 \"Built-in video\" (.Display_Video_Apple_Control)"
  28.  
  29. The computer model appears only if the slot==0, which indicates built-in video. 
  30. Quotes embrace the card name, and parentheses embrace the driver name.
  31. The driver's version number appears only if it's non-zero.
  32.  
  33. REFERENCE:
  34. TechNote 1083 Weak-linking to a Code Fragment Manager-based shared library
  35. http://devworld.apple.com/dev/technotes/tn/tn1083.html
  36.  
  37. HISTORY:
  38. 1/29/92    dgp wrote IdentifyCompiler.
  39. 2/25/92    dgp    wrote IdentifyMachine.
  40. 8/26/92    dgp get owner and model name from the System file
  41. 2/20/93    dgp    added ROM version
  42. 2/27/93    dgp merged IdentifyCompiler.c and IdentifyMachine.c into the new Identify.c,
  43.             and added IdentifyVideo.
  44. 4/25/93    dgp    IdentifyMachine reports 24/32-bit addressing.
  45. 12/14/93 dgp If THINK C compiler version is "5" I report "5 or 6.0" since Symantec
  46.             forgot to increment the version number when they released 6.0. They
  47.             fixed this in version 6.01.
  48. 3/2/94    dgp    Extracted IdentifyOwner from within IdentifyMachine
  49. 7/28/94 dgp added support for Metrowerks CodeWarrior C compiler and PowerPC.
  50.             Eliminated use of "#s" printf format, since it's not supported by
  51.             Metrowerks CodeWarrior C.
  52. 9/5/94 dgp     removed assumption in printf's that int==short.
  53. 9/6/94    dgp    Enhanced IdentifyCompiler to specify the size of int.
  54. 9/6/94    dgp    Disable cache checks on PowerPC, because those traps are undefined.
  55. 10/5/94 dgp Removed all floating point code from IdentifyCompiler().
  56. 11/5/94 dgp Suppressed ROM info in Identify.
  57. 11/9/94 dgp added IdentifyApplication().
  58. 1/15/95 dgp added test for version 5 of CodeWarrior
  59. 5/24/95 dgp added test for version 6 of CodeWarrior
  60. 6/14/95 dgp added GetCPUSpeed() to IdentifyMachine() and abbreviated the report, 
  61.         omitting a lot of boring stuff.
  62. 6/27/95 dgp adjusted UNIVERSAL_HEADERS conditional for GMT struct, to use old
  63.         structure for version 1 universal headers.
  64. 7/20/95    dgp once again, made compatible with pre-universal headers
  65. 8/10/95 dgp updated IdentifyCompiler() for Symantec C.
  66. 9/26/95 dgp updated IdentifyCompiler() for CW7.
  67. 10/5/95 dgp minor editing of IdentifyVideo().
  68. 10/14/95 dgp added support for PCI Macs, using the NameRegistry. Since System 7.5 fails to
  69.         adequately name most Macs, I've reverted to using a built-in table of names in 
  70.         IdentifyModel. IdentifyMachine now also prints the Gestalt machine number to 
  71.         resolve any residual ambiguities.
  72.         IdentifyModel, IdentifyVideo, and IdentifyMachine now give complete information on all
  73.         Macs, provided that when running on PCI Macs they are compiled as PPC code. This 
  74.         limitation is due to the (easy) way that I implemented the calls to Apple's 
  75.         NameRegistryLib. The NameRegistry-related routines appear in a long self-contained
  76.         section at the end of this file.
  77. 10/24/95 dgp added pclk and bclk Gestalt calls, based on gestalt-selectors-31.
  78. 12/4/95 dgp As requested by David Brainard, changed "char *machineName[]" to 
  79.         "char machineName[][32]" so that it would compile as a code resource.
  80. 3/7/96 dgp report system version number in standard  format, e.g. "7.5.3".
  81. 3/18/96 dgp IdentifyModel() now always reports actual clock speed if known.
  82. 5/14/96 dgp Added mach codes for new Macs from GestaltSelectorList 3.3.5.
  83. 5/28/96 dgp Added conditional UNIVERSAL_INTERFACES_VERSION>=0x0212
  84. 2/19/97 dhb Lobotomized this file for case where both MATLAB and THINK_C are
  85.             true. SCREEN.c is over the limit in terms of total code resource
  86.             size that THINK_C can support. This file was huge in terms of its
  87.             data segment, but we weren't actually using much of it. The first
  88.             conditional below puts in just what we need to get the link to work,
  89.             then the old file follows.
  90. 3/8/97    dgp    many minor changes. Added IdentifyProcessor, IdentifyFpu.
  91. 3/19/97    dgp    IdentifyCompiler: divide by 10 in identifying CW compiler.
  92. 4/10/97    dgp    Use latest constants in IdentifyProcessor, as suggested by code snippet
  93.             in April, 1997, Apple Developer CD-ROM.
  94. 4/10/97    dgp    Eliminate stuff that was conditional on !UNIVERSAL_HEADERS.
  95. 7/9/97    dgp    Simplified the test for availability of NameRegistryLib, based on TN1083.
  96. */
  97. #include "VideoToolbox.h"
  98. char *IdentifyProcessor(void);
  99. char *IdentifyFpu(void);
  100.  
  101. #ifndef __TRAPS__
  102.     #include <Traps.h>        // _HWPriv
  103. #endif
  104. #include <Power.h>
  105. #include <LowMem.h>
  106. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache);
  107. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *modelName);
  108. char *GetDeviceSlotName(GDHandle device);
  109. #define gestaltBusClockSpeed 'bclk'    /* gestalt-selectors-31.etx */
  110. #define gestaltCPUClockSpeed 'pclk'    /* gestalt-selectors-31.etx */
  111.  
  112. char *IdentifyOwner(void)
  113. {
  114.     static char string[64];
  115.     unsigned char **owner;
  116.     
  117.     string[0]=0;
  118.     owner=GetString(-16096);    // Get owner's name from System file
  119.     if(owner!=NULL && *owner[0]>0){
  120.         memcpy(string,*owner,(*owner)[0]+1);
  121.         p2cstr((unsigned char *)string);
  122.     }
  123.     return string;
  124. }
  125.  
  126. char *IdentifyProcessor(void)
  127. {
  128.     char *processorName;
  129.     long sysArchitecture=gestalt68k,processor=1;                /* default */
  130.     int error;
  131.  
  132.     error=Gestalt(gestaltSysArchitecture,&sysArchitecture);
  133.     switch(sysArchitecture){
  134.         case gestalt68k:
  135.             error=Gestalt(gestaltProcessorType,&processor);
  136.             switch(processor){
  137.                 case gestalt68000: processorName="68000";break;
  138.                 case gestalt68010: processorName="68010";break;
  139.                 case gestalt68020: processorName="68020";break;
  140.                 case gestalt68030: processorName="68030";break;
  141.                 case gestalt68040: processorName="68040";break;
  142.                 default: processorName="unknown 68K processor";break;
  143.             }
  144.             break;
  145.         case gestaltPowerPC:
  146.             error=Gestalt(gestaltNativeCPUtype,&processor);
  147.             switch(processor){
  148.                 case gestaltCPU601: processorName="ppc601";break;
  149.                 case gestaltCPU603: processorName="ppc603";break;
  150.                  case 0x106:         processorName="ppc603e";break;/* PowerBook 5300 */
  151.                 case gestaltCPU604: processorName="ppc604";break;
  152.                 default: processorName="unknown PowerPC processor";break;
  153.             }
  154.             break;
  155.         default:
  156.             processorName="unknown processor";
  157.             break;
  158.     }
  159.     return processorName;
  160. }
  161.  
  162. char fpuName[][32]={
  163.     "no floating point unit","68881","68882","built-in floating point"
  164.     ,"unknown floating point unit","Software FPU"
  165. };
  166.  
  167. char *IdentifyFpu(void)
  168. {
  169.     long fpu,value;
  170.     int fpus=sizeof(fpuName)/sizeof(fpuName[0]);
  171.     static char fpuString[32];
  172.  
  173.     Gestalt(gestaltFPUType,&fpu);
  174.     if(fpu<0 || fpu>=fpus)fpu=fpus-2;
  175.     if(fpu==1 && Gestalt('FPUE',&value)==0)fpu=fpus-1;// Software FPU by John Neil & Assoc.
  176.     if(fpu>0)sprintf(fpuString,", %s",fpuName[fpu]);
  177.     else fpuString[0]=0;
  178.     return fpuString;
  179. }
  180.  
  181. char *IdentifyVM(void)
  182. {
  183.     // Based on gestalt-selectors-29.etx, available from Info-Mac.
  184.     Boolean vm;            // VM is on, possibly due to RAM Doubler
  185.     Boolean ramDoubler;    // RAM Doubler is on.
  186.     long value;
  187.     int error;
  188.     char *s="";
  189.     
  190.     error=Gestalt(gestaltVMAttr,&value);
  191.     vm=!error && value&(1L<<gestaltVMPresent);
  192.     error=Gestalt('vmem',&value);
  193.     ramDoubler=!error && value=='RaM2';
  194.     if(vm){
  195.         if(!ramDoubler)s="Using Virtual Memory. ";
  196.         else s="Using RAM Doubler. ";
  197.     }
  198.     return s;
  199. }
  200.  
  201. #if MATLAB && THINK_C // Save space. See 2/19/97 comment above.
  202.  
  203. char *IdentifyModel(void)
  204. {
  205.     int error;
  206.     long machine=0;
  207.     static char string[64];
  208.  
  209.     /* Before System 7.5 the MacOS supplied the model name. Now it merely supplies a
  210.         generic name, like "Power Macintosh". */
  211.     error=Gestalt(gestaltMachineType,&machine);
  212.     if(!error){
  213.         GetIndString((unsigned char *)string,kMachineNameStrID,machine);
  214.         p2cstr((unsigned char *)string);
  215.     }
  216.     return string;
  217. }
  218.  
  219. #else // See 2/19/97 comment above.
  220.  
  221. // System 7.5 took a step backwards and lacks
  222. // specific names for many machines, so I used Rene Ros's Gestalt Selectors List
  223. // to list all currently available Macs.
  224. char machineName[][36]={
  225.     "Unknown Mac"
  226.     ,"Mac 128K"
  227.     ,"Mac XL"
  228.     ,"Mac 512KE"
  229.     ,"Mac Plus"
  230.     ,"Mac SE"
  231.     ,"Mac II"
  232.     ,"Mac IIx"
  233.     ,"Mac IIcx"
  234.     ,"Mac SE-30"
  235.     ,"Mac Portable"
  236.     ,"Mac IIci"
  237.     ,"Unknown Mac"    /* 12 */
  238.     ,"Mac IIfx"
  239.     ,"Unknown Mac"
  240.     ,"Unknown Mac"
  241.     ,"Unknown Mac"
  242.     ,"Mac Classic","Mac IIsi","Mac LC","Mac Quadra 900","PowerBook 170"
  243.     ,"Mac Quadra 700","Mac Classic II","PowerBook 100","PowerBook 140","Quadra 950"
  244.     ,"Mac LC III"
  245.     ,"Unknown Mac"
  246.     ,"Mac PowerBook 210"
  247.     ,"Mac Centris 650"
  248.     ,"Unknown Mac"
  249.     ,"Mac PowerBook 230"
  250.     ,"Mac PowerBook 180"
  251.     ,"Mac PowerBook 160"
  252.     ,"Mac Quadra 800"    /* 35 */
  253.     ,"Mac Quadra 650"
  254.     ,"Mac LC II"
  255.     ,"Mac PowerBook Duo 250"
  256.     ,"PowerMac 9150"
  257.     ,"PowerMac 8100/110"    /* 40 */
  258.     ,"PowerMac 5200"    /* 41 */
  259.     ,"Unknown Mac"
  260.     ,"Unknown Mac"
  261.     ,"Mac IIvi"
  262.     ,"Mac Performa 600"
  263.     ,"Unknown Mac"
  264.     ,"PowerMac 7100/80"    /* 47 */
  265.     ,"Mac IIvx"
  266.     ,"Mac Color Classic"
  267.     ,"Mac PowerBook 165c"
  268.     ,"Unknown Mac"
  269.     ,"Mac Centris 610"
  270.     ,"Mac Quadra 610"
  271.     ,"Mac PowerBook 145,145b"
  272.     ,"PowerMac 8100/100"    /* 55 */
  273.     ,"Mac LC 520"
  274.     ,"Unknown Mac"
  275.     ,"Unknown Mac"
  276.     ,"Unknown Mac"
  277.     ,"Mac Centris 660AV"
  278.     ,"Unknown Mac"
  279.     ,"Mac Performa 46x"
  280.     ,"Unknown Mac"
  281.     ,"Unknown Mac"
  282.     ,"PowerMac 8100/80"    /* 65 */
  283.     ,"Unknown Mac"
  284.     ,"PowerMac 9500"    /* 67 */
  285.     ,"PowerMac 7500,7600"    /* 68 */
  286.     ,"PowerMac 8500"    /* 69 */
  287.     ,"Unknown Mac"
  288.     ,"Mac PowerBook 180c"
  289.     ,"Mac PowerBook 500,520,520c,540,540c" /* 72 */
  290.     ,"Unknown Mac"
  291.     ,"Unknown Mac"
  292.     ,"PowerMac 6100/60"    /* 75 */
  293.     ,"Unknown Mac"
  294.     ,"Mac PowerBook Duo 270c"
  295.     ,"Mac Quadra 840AV"
  296.     ,"Unknown Mac"
  297.     ,"Mac LC/Performa 550"
  298.     ,"Unknown Mac"
  299.     ,"Unknown Mac"
  300.     ,"Unknown Mac"
  301.     ,"Mac PowerBook 165"
  302.     ,"Unknown Mac"
  303.     ,"Unknown Mac"
  304.     ,"Unknown Mac"
  305.     ,"Mac TV"
  306.     ,"Mac LC/Performa 475"
  307.     ,"Unknown Mac"
  308.     ,"Unknown Mac"
  309.     ,"Mac LC 575"
  310.     ,"Unknown Mac"
  311.     ,"Mac Quadra 605"
  312.     ,"Unknown Mac"
  313.     ,"Unknown Mac"
  314.     ,"Unknown Mac"
  315.     ,"Mac LC/Quadra 630"
  316.     ,"Unknown Mac"
  317.     ,"PowerMac 6100/66"    /* 100 */
  318.     ,"PowerMac 6100"    /* 80 MHz, discovered by clock chippers */
  319.     ,"Mac PowerBook Duo 280"
  320.     ,"Mac PowerBook Duo 280c"
  321.     ,"PowerMac LC/Performa 475"
  322.     ,"PowerMac LC/Performa 575"
  323.     ,"PowerMac LC/Quadra 630"
  324.     ,"Unknown Mac"
  325.     ,"PowerMac 7200"    /* 108, also PowerMac 8200/100 */
  326.     ,"Unknown Mac"
  327.     ,"Unknown Mac"
  328.     ,"Unknown Mac"
  329.     ,"PowerMac 7100/66"    /* 112 */
  330.     ,"PowerMac 7100"    /* 80 MHz, discovered by clock chippers */
  331.     ,"Unknown Mac"
  332.     ,"Mac PowerBook 150"
  333.     ,"PowerMac Quadra700"
  334.     ,"PowerMac Quadra900"
  335.     ,"PowerMac Quadra950"
  336.     ,"PowerMac Centris610"
  337.     ,"PowerMac Centris650"
  338.     ,"PowerMac Quadra610"
  339.     ,"PowerMac Quadra650"
  340.     ,"PowerMac Quadra800"
  341.     ,"PowerBook 2300c"    /* 124 */
  342.     ,"Unknown Mac"
  343.     ,"Unknown Mac"
  344.     ,"Unknown Mac"
  345.     ,"PowerBook 5300"
  346. };
  347.  
  348. char *IdentifyCompiler(void)
  349. {
  350.     static char string[200];
  351.     char *compiler,*longs,*floating,version[4],*universal;
  352.     double v=0;
  353.     Boolean fractionalVersion=0;
  354.     
  355.     string[0]=0;
  356.     compiler="";
  357.     #if THINK_C && !SYMANTEC_C
  358.         compiler="Symantec THINK C";
  359.         if(THINK_C==1)v=4;
  360.         else v=THINK_C;
  361.     #endif
  362.     #if THINK_CPLUS || SYMANTEC_C
  363.         #if __cplusplus || THINK_CPLUS
  364.             compiler="Symantec C++";
  365.         #else
  366.             compiler="Symantec C";
  367.         #endif
  368.         #if THINK_CPLUS
  369.             v=THINK_CPLUS;
  370.         #else
  371.             fractionalVersion=1;
  372.             v=SYMANTEC_C/0x100 + 0.1*(SYMANTEC_C/0x10%0x10) + 0.01*(SYMANTEC_C%0x10);
  373.         #endif
  374.     #endif
  375.     #if applec
  376.         compiler="MPW C";
  377.     #endif
  378.     #if __MWERKS__
  379.         compiler="Metrowerks CodeWarrior C";
  380.         #if __MWERKS__>1
  381.             fractionalVersion=1;
  382. // The Inside CodeWarrior 10 book, p. CL-196, says this should work.
  383.             v=__MWERKS__/0x100 + 0.1*(__MWERKS__/0x10%0x10) + 0.01*(__MWERKS__%0x10);
  384. // But in CW10 that returns v==16.00, whereas I get 1.6 from a Finder Get Info on 
  385. // the C/C++ compiler plug in. So I divide by 10.
  386.             v=v/10.;
  387.         #else
  388.             #if defined(__MC68K__)||defined(__POWERPC__)||defined(__INTEL__)
  389.                 v=6;
  390.             #else
  391.                 if(!iscntrl(0))v=4;    // bug in version 4.5 and earlier
  392.                 else v=5;
  393.             #endif
  394.         #endif
  395.     #endif
  396.     if(v>0){
  397.         if(fractionalVersion)sprintf(version,"%.2f ",v);
  398.         else sprintf(version,"%.0f ",v);
  399.     }else sprintf(version,"");
  400.     if(GENERATING68020)longs="68020";
  401.     else{
  402.         #if GENERATINGPOWERPC
  403.             longs="PowerPC";
  404.         #else
  405.             longs="68000";
  406.         #endif
  407.     }
  408.     if(GENERATING68881)floating=" and 68881";
  409.     else floating="";
  410.     universal="";
  411.     if(sizeof(double)==12){
  412.         #if THINK_C
  413.             #if !__option(native_fp)
  414.                 universal="\"universal\"-format ";
  415.             #endif
  416.         #endif
  417.     }
  418.     sprintf(string,"Compiled by %s %sfor %s%s, %ldB int and %s%ldB double."
  419.         ,compiler,version,longs,floating,sizeof(int),universal,sizeof(double));
  420.     return string;
  421. }
  422.  
  423. char *IdentifyModel(void)
  424. {
  425.     int error;
  426.     long machine=0,value;
  427.     int machines=sizeof(machineName)/sizeof(machineName[0]);
  428.     static char string[64];
  429.     char cpuName[32]="";
  430.     long cpuHz=0,busHz=0;
  431.     Boolean hasL2Cache,powerMgrExists;
  432.     
  433.     #if 0
  434.         /* Before System 7.5 the MacOS supplied the model name. Now it merely supplies a
  435.             generic name, like "Power Macintosh". */
  436.         error=Gestalt(gestaltMachineType,&machine);
  437.         if(!error){
  438.             GetIndString((unsigned char *)string,kMachineNameStrID,machine);
  439.             p2cstr((unsigned char *)string);
  440.             if(strlen(string)==0){
  441.                 if(machine<0 || machine>=machines)machine=0;
  442.                 sprintf(string,"%s",machineName[machine]);
  443.             }
  444.         }
  445.     #endif
  446.     /* The model names in the machineName[] table include clock speed, where appropriate,
  447.     for the pre-PCI Macs, e.g. 6100/60, but omit clock speed for the PCI Macs, since 
  448.     PCI Macs must run System 7.5.2 or better, which include gestaltCPUClockSpeed, whereas
  449.     pre-PCI Macs may be running 7.5.1 which did not include gestaltCPUClockSpeed. 
  450.     If the actual clock speed is available we always use it, overwriting the nominal speed if
  451.     necessary. */
  452.     error=Gestalt(gestaltMachineType,&machine);
  453.     strcpy(string,machineName[machine]);
  454.     GetCPUProperties(cpuName,&cpuHz,&hasL2Cache);    // useful only on PCI Macs
  455.     error=Gestalt(gestaltBusClockSpeed,&busHz);        // requires System 7.5.2 or better
  456.     error=Gestalt(gestaltCPUClockSpeed,&cpuHz);        // requires System 7.5.2 or better
  457.     if(cpuHz==0){
  458.         Gestalt(gestaltPowerMgrAttr,&value);        // Only on powerbooks I think.
  459.         powerMgrExists=value&(1L<<gestaltPMgrExists);
  460.         if(powerMgrExists)cpuHz=1000000*GetCPUSpeed();
  461.     }
  462.     if(cpuHz>0){
  463.         int i=strlen(string);
  464.         // remove the nominal speed, if present, before appending the actual speed
  465.         if(i>4 && string[i-4]=='/' && isdigit(string[i-3]) && isdigit(string[i-2]) && isdigit(string[i-1]))
  466.             string[i-4]=0;
  467.         if(i>3 && string[i-3]=='/' && isdigit(string[i-2]) && isdigit(string[i-1]))
  468.             string[i-3]=0;
  469.         sprintf(string,"%s/%ld",string,cpuHz/1000000);
  470.     }
  471.     return string;
  472. }
  473.  
  474. char *IdentifyVideo(GDHandle device)
  475. // E.g. "PowerBook 170 \"Macintosh D Built-In Video\" (.Display_Video_Apple_TIM)"
  476. {
  477.     static char string[256];
  478.     long quickDraw;
  479.     char *slotName;
  480.  
  481.     string[0]=0;
  482.     Gestalt(gestaltQuickdrawVersion,&quickDraw);
  483.     if(quickDraw<gestalt8BitQD){
  484.         sprintf(string,"%s ",IdentifyModel());
  485.         sprintf(string,"%s\"%s\"",string,"1-bit QuickDraw");
  486.     }else{
  487.         slotName=GetDeviceSlotName(device);
  488.         if(strlen(slotName)==0 || strcmp(slotName,"0")==0)sprintf(string,"%s ",IdentifyModel());
  489.         sprintf(string,"%s\"%s\"",string,GDCardName(device));
  490.         if(GDVersion(device)==0)sprintf(string,"%s (%s)",string,GDNameStr(device));
  491.         else sprintf(string,"%s (%s version %d)"
  492.             ,string,GDNameStr(device),(int)GDVersion(device));
  493.         if(strlen(slotName)>0 && strcmp(slotName,"0")!=0)sprintf(string,"%s slot %s",string,slotName);
  494.     }
  495.     return string;
  496. }
  497.  
  498. char *IdentifyApplication(void)    // Returns name of application.
  499. {
  500.     static char string[32];
  501.  
  502.     memcpy(string,LMGetCurApName(),32);
  503.     p2cstr((unsigned char *)string);
  504.     return string;
  505. }
  506.  
  507. char *IdentifyMachineAndType(void);
  508.  
  509. char *IdentifyMachineAndType(void)
  510. {
  511.     int error;
  512.     long machine=0;
  513.     char *s;
  514.  
  515.     s=IdentifyMachine();
  516.     error=Gestalt(gestaltMachineType,&machine);
  517.     sprintf(s,"%s GestaltMachineType %ld.",s,machine);
  518.     return s;
  519. }
  520.  
  521. char *IdentifyMachine(void)
  522. {
  523.     int error;
  524.     long processor=-1,system,value;
  525.     static char string[256];
  526.     Boolean cacheData=1,cacheInstructions=1;
  527.     long romSize,romVersion,qD;
  528.     char *owner;
  529.     char cpuName[32]="";
  530.     long cpuHz=0,busHz=0;
  531.     Boolean hasL2Cache;
  532.     char *cache;
  533.     
  534.     GetCPUProperties(cpuName,&cpuHz,&hasL2Cache);    /* works only on PCI Macs */
  535.     error=Gestalt(gestaltBusClockSpeed,&busHz);        /* works only on PCI Macs */
  536.     error=Gestalt(gestaltCPUClockSpeed,&cpuHz);        /* works only on PCI Macs */
  537.     string[0]=0;
  538.     error=Gestalt(gestaltSystemVersion,&system);
  539.     if(error)return string;                        /* Gestalt not available */
  540.     owner=IdentifyOwner();
  541.     if(strlen(owner)>0)sprintf(string,"%s's ",owner);
  542.     sprintf(string,"%s%s",string,IdentifyModel());
  543.     if(strlen(cpuName)>0){
  544.         if(hasL2Cache)cache=", L2 cache";
  545.         else cache="";
  546.     }else cache="";
  547.     sprintf(string,"%s, %s%s%s, System %lx.%lx"
  548.         ,string,IdentifyProcessor(),cache,IdentifyFpu(),system/0x100,system%0x100/0x10);
  549.     system%=0x10;
  550.     if(system)sprintf(string,"%s.%lx",string,system);
  551.     sprintf(string,"%s.",string);
  552.     if(0){    // The rest is boring, so I've disabled it.
  553.         Gestalt(gestaltROMSize,&romSize);
  554.         Gestalt(gestaltROMVersion,&romVersion);
  555.         sprintf(string,"%s, %ldK ROM version %ld+%ld*256."
  556.             ,string,romSize/1024,romVersion%256,romVersion/256);
  557.         value=0;
  558.         Gestalt(gestaltAddressingModeAttr,&value);
  559.         {
  560.             if(value&1L<<gestalt32BitAddressing)sprintf(string,"%s 32-bit addressing.",string);
  561.             else sprintf(string,"%s 24-bit addressing.",string);
  562.         }
  563.         Gestalt(gestaltQuickdrawVersion,&qD);
  564.         switch(qD/0x100){
  565.             case 0:
  566.                 sprintf(string,"%s 1-bit QuickDraw.",string);
  567.                 break;
  568.             case 1:
  569.                 sprintf(string,"%s 8-bit QuickDraw.",string);
  570.                 break;
  571.             case 2:
  572.                 sprintf(string,"%s 32-bit QuickDraw 1.%02lx.",string,qD%0x100);
  573.                 break;
  574.             default:
  575.                 sprintf(string,"%s QuickDraw 0x%lx.",string,qD);
  576.                 break;
  577.         }
  578.     }
  579.     return string;
  580. }
  581.  
  582. char *IdentifyGreenwichMeanTime(void)
  583. {
  584.     MachineLocation location;
  585.     long t;
  586.     double hours;
  587.     static char s[64];
  588.  
  589.     ReadLocation(&location);
  590.     if(location.latitude!=0 || location.longitude!=0){
  591.         #if UNIVERSAL_HEADERS>1
  592.             t=location.u.gmtDelta & 0x00FFFFFF;
  593.         #else
  594.             t=location.gmtFlags.gmtDelta & 0x00FFFFFF;
  595.         #endif
  596.         if(t & 0x800000)t|=0xFF000000;
  597.         hours=fabs(t/3600.0);
  598.         if(hours==floor(hours))sprintf(s,"%.0f",hours);
  599.         else sprintf(s,"%.1f",hours);
  600.         if(t<0)sprintf(s,"%s hours west of Greenwich mean time.",s);
  601.         else sprintf(s,"%s hours east of Greenwich mean time.",s);
  602.     }else s[0]=0;
  603.     return s;
  604. }
  605.  
  606. #endif    // !(MATLAB && THINK_C)
  607.  
  608. /************************************************************************/
  609.  
  610. /*
  611. Using Apple's NameRegistry
  612.  
  613. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *cardModel);
  614. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache);
  615.  
  616. Two routines that use the new Name Registry (available only on PCI PowerMacs) to
  617. determine the cpu clock rate, presence of L2 cache, and the name of the PCI video
  618. card. The routines return zeroes if the Name Registry is not available (i.e. on
  619. non-PCI Macs), or if compiled as 68k code.
  620.  
  621. WARNING: to effectively use Apple's NameRegistry in your project you MUST have Apple's
  622. NameRegistryLib, which contains the glue code to access the Mac OS. NameRegistryLib 
  623. is on the Metrowerks CodeWarrior CD-ROM; add it to your project and use the pop-up 
  624. menu to select "import 'weak'".) You don't need
  625. NameRegistry.h, because Identify.c includes the needed definitions, but you do need
  626. NameRegistryLib. Alternatively,
  627. you may set the flag NO_NAME_REGISTRY_LIB to true, which will disable the rest of 
  628. this file, but allows you to link and run your programs.
  629.  
  630. Once compiled and linked with the NameRegistryLib glue, this code will run on any
  631. PCI PowerMac. A temporary #if section takes care of the missing header file, so
  632. you'll be able to compile, but I don't think you'll be able to link this unless
  633. you have NameRegistryLib. 
  634.  
  635. HISTORY:
  636. 10/13/95 dgp wrote GetVideoProperties() and GetCPUProperties(), based on general
  637.         advice from Apple (below) and 
  638.         the PCISlotsUtils.cp source file for Apple's "PCI Slot Peek" demo,
  639.         in the PCI developer's kit included on the September '95 Developer's CD. 
  640.  
  641. Hi Denis
  642.  
  643. Thank you for your link (ID# 151426) concerning how to detect a L2 cache and
  644. get the processor speed.
  645.  
  646. Let's look at the cache first. Before the introduction of the PCI Power
  647. Macintosh computers there was no API to detect the presents of this cache. On
  648. a PCI Macintosh there is the Name Registry. This registry contains this
  649. information in the form of a property. The property name is "name" and if
  650. there is a L2 cache the property value is "l2-cache". Look at the Developer CD
  651. for Sept 95. In the What's New folder there is the PCI DDK. There is a
  652. program called Display Name Registry that will show what I have just explained.
  653. Of course it must run on a PCI machine. There is also a document called
  654. "Designing PCI Cards&Drivers...." that shows examples of using the Name
  655. Registry to search for system resources.
  656.  
  657. Now the CPU speed. Like the last case, before PCI Macs there was no direct
  658. call that could be made to determine CPU speed. There are Gestalt calls for
  659. CPU type if that's any help. However, there are CPU accelerator cards so that
  660. would present a problem. The two calls you mentioned (GetCPUSPeed &
  661. MaximumProcessSpeed) are both for portable Macs that use the Power Manager.
  662. The traps for these calls are not implemented on other Macs. At least not the
  663. 9500 that I tried. However, PCI Macs and the Name Registry contain this
  664. information also. Again go to Display Name Registry and look at the
  665. Devices:device-tree;PowerPC,604 property. Expand it and there is an additional
  666. sub property called "clock-frequency". My 9500 show 132000000.
  667.  
  668. So you have what you want but starting with PCI Macs.
  669.  
  670. Regards
  671.  
  672. Wayne Flansburg
  673. Developer Technical Support
  674. Apple Computer
  675. 12 October 1995
  676. */
  677.  
  678. #ifndef __CODEFRAGMENTS__
  679.     #include <CodeFragments.h>
  680. #endif
  681. // The version macro UNIVERSAL_INTERFACES_VERSION is defined in Apple's ConditionalMacros.h,
  682. // but only since version 2.1 (which was distributed with CodeWarrior 8).
  683. #if UNIVERSAL_INTERFACES_VERSION>=0x0212
  684.     #ifndef __NAMEREGISTRY__
  685.         #include <NameRegistry.h>
  686.     #endif
  687. #else
  688.     #if !defined(NO_NAME_REGISTRY_LIB) && !defined(__NAMEREGISTRY__)
  689.         // The following definitions and prototypes, copied from NameRegistry.h, are just 
  690.         // enough to allow compilation of this file without the header file NameRegistry.h. However, 
  691.         // you'll still need NameRegistryLib in order to link. 
  692.         // NameRegistryLib is in Metrowerks CodeWarrior's 
  693.         // "Metrowerks CodeWarrior:MacOS Support:Libraries:MacOS PPC:" folder.
  694.         typedef UInt32 RegIterationOp;
  695.         typedef RegIterationOp RegEntryIterationOp;
  696.         struct RegEntryIter {
  697.             void *opaque;
  698.         };
  699.         typedef struct RegEntryIter *RegEntryIterPtr;
  700.         typedef struct RegEntryIter RegEntryIter;
  701.         struct RegEntryID {
  702.             UInt8 opaque[16];
  703.         };
  704.         typedef struct RegEntryID RegEntryID, *RegEntryIDPtr;
  705.         typedef char RegCStrPathName;
  706.         typedef UInt32 RegPathNameSize;
  707.         enum {
  708.             kRegMaximumPropertyNameLength = 31,                            /* Max length of Property Name */
  709.             kRegPropertyNameTerminator    = 0x00,                            /* '\0' */
  710.             kRegIterContinue = 0x1UL
  711.         };
  712.         typedef char RegPropertyName, *RegPropertyNamePtr, RegPropertyNameBuf[kRegMaximumPropertyNameLength + 1];
  713.         typedef UInt32 RegPropertyValueSize;
  714.         extern OSStatus RegistryEntryIterateCreate(RegEntryIter *cookie);
  715.         extern OSStatus RegistryEntryIterateDispose(RegEntryIter *cookie);
  716.         extern OSStatus RegistryEntryIterate(RegEntryIter *cookie, RegEntryIterationOp relationship, RegEntryID *foundEntry, Boolean *done);
  717.         extern OSStatus RegistryEntryToPathSize(const RegEntryID *entryID, RegPathNameSize *pathSize);
  718.         extern OSStatus RegistryCStrEntryToPath(const RegEntryID *entryID, RegCStrPathName *pathName, RegPathNameSize pathSize);
  719.         extern OSStatus RegistryPropertyGetSize(const RegEntryID *entryID, const RegPropertyName *propertyName, RegPropertyValueSize *propertySize);
  720.         extern OSStatus RegistryPropertyGet(const RegEntryID *entryID, const RegPropertyName *propertyName, void *propertyValue, RegPropertyValueSize *propertySize);
  721.         extern OSStatus RegistryEntryIDDispose(RegEntryID *id);
  722.     #endif
  723. #endif
  724. #include <stdio.h>
  725. #include <string.h>
  726. /*
  727.     RegPropertyModifiers propertyModifiers;
  728.     if(0 && !error){
  729.         error=RegistryPropertyGetMod(&entry,foundProperty,&propertyModifiers);
  730.     }
  731. */
  732.  
  733. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *cardModel);
  734. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache);
  735.  
  736. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *cardModel)
  737. {
  738. #if NO_NAME_REGISTRY_LIB || !GENERATINGPOWERPC
  739.     device;
  740.     slotName[0]=0;
  741.     cardName[0]=0;
  742.     cardModel[0]=0;
  743.     return;
  744. #else
  745.     /*
  746.     Assume we're running native on ppc.
  747.     If this were compiled as 68k code, the calls to the NameRegistryLib, which is PPC code, 
  748.     would require intervention by the Mixed Mode Manager,
  749.     and I decided not to bother with that complication, since there seems little point in
  750.     compiling programs as 68k code to run on a PowerPC. TimeVideo is distributed as a fat 
  751.     binary, so it always runs native.
  752.     */
  753.     OSStatus error;
  754.     RegEntryIter cookie;
  755.     Boolean done;
  756.     RegEntryIterationOp    op;
  757.     RegEntryID entry;
  758.     RegCStrPathName *pathName;
  759.     RegPathNameSize pathNameSize;
  760.     RegPropertyNameBuf foundProperty;
  761.     RegPropertyValueSize propertySize;
  762.     long templong;
  763.     short driverRef;
  764.     
  765.     slotName[0]=0;
  766.     cardName[0]=0;
  767.     cardModel[0]=0;
  768.     if(device==NULL)return;
  769.     {
  770.         /*
  771.         To determine whether you're running on a PCI Mac, you must first check if the
  772.         NameRegistry exists. There is a Gestalt Manager selector for
  773.         this, 'nreg'. If it exists, the value returned will be the version number
  774.         (number 1 for now) of the Name Registry. Otherwise, you should get a
  775.         gestaltUndefSelectorErr. If the Name Registry does not exist then you should
  776.         assume that it is not a PCI based machine; it may have NuBus or no bus at all.
  777.         */
  778.         error=Gestalt(gestaltNameRegistryVersion,&templong);
  779.         if(error)return;    // No PCI slots
  780.         /*
  781.         *** I didn't add this yet --- dgp ***
  782.         You can use the Name Registry to determine if a PCI bus exists. To do this you
  783.         should use RegistryEntrySearch to locate an entry/node having a known
  784.         property/value, propertyName = "devic-type", and propertyValue = "pci"  If an
  785.         entry is found that has that known property/value (noErr and done = "False"),
  786.         there is a PCI bus.
  787.         Wayne Flansburg
  788.         DTS
  789.         Apple Computer
  790.         1/26/96
  791.         */
  792.     }
  793.     /*
  794.     {
  795.         // If NameRegistryLib is weak-linked, it may be missing at runtime, so we
  796.         // check for the library, to prevent a crash if we try to access the library's exports.
  797.         CFragConnectionID connID;
  798.         Ptr mainAddr;
  799.         Str255 errName;
  800.         
  801.         error=Gestalt(gestaltCFMAttr,&templong);    // Code Fragment Manager?
  802.         if(!error)error=GetSharedLibrary((ConstStr63Param)"\pNameRegistryLib"
  803.             ,kAnyCFragArch,kFindCFrag,&connID,&mainAddr,errName);
  804.         if(error)return;    // no NameRegistryLib
  805.     }
  806.     */
  807.     if((Ptr)RegistryEntryIterate == (Ptr)kUnresolvedCFragSymbolAddress)return;// no NameRegistryLib
  808.     op=kRegIterContinue;
  809.     error=RegistryEntryIterateCreate(&cookie);
  810.     if(!error){
  811.         done=FALSE;
  812.         while (!error && !done){
  813.             error=RegistryEntryIterate(&cookie,op,&entry,&done);
  814.             if(!error && !done){
  815.                 pathName=NULL;
  816.                 error=RegistryEntryToPathSize(&entry,&pathNameSize);
  817.                 if(!error){
  818.                     pathName=(RegCStrPathName *) NewPtr(pathNameSize);
  819.                     if(pathName==NULL)error=MemError();
  820.                 }
  821.                 if(!error){
  822.                     error=RegistryCStrEntryToPath(&entry,pathName,pathNameSize);
  823.                     //printf("%s\n",pathName);    // debugging
  824.                 }
  825.                 if(!error){
  826.                     // Found a video device
  827.                     strcpy(foundProperty,"driver-ref");
  828.                     propertySize=0;
  829.                     error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  830.                     if(!error){
  831.                         assert(propertySize==sizeof(driverRef));
  832.                         error=RegistryPropertyGet(&entry,foundProperty,&driverRef,&propertySize);
  833.                         if(!error && driverRef==(*device)->gdRefNum){
  834.                             done=TRUE; // we found the correct driver
  835.                             strcpy(foundProperty,"name");
  836.                             propertySize=0;
  837.                             error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  838.                             if(!error)error=RegistryPropertyGet(&entry,foundProperty,cardName,&propertySize);
  839.                             strcpy(foundProperty,"model");
  840.                             propertySize=0;
  841.                             error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  842.                             if(!error)error=RegistryPropertyGet(&entry,foundProperty,cardModel,&propertySize);
  843.                             propertySize=0;
  844.                             strcpy(foundProperty,"AAPL,slot-name");
  845.                             error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  846.                             if(!error)error=RegistryPropertyGet(&entry,foundProperty,slotName,&propertySize);
  847.                             // PCI slot names on Power Macintosh currently are of form : A1,B1,C1,D2,E2,F2
  848.                         }
  849.                     }else error=noErr;    // there is a card but NO display attached (no open driver)
  850.                 }
  851.                 if(pathName != NULL)DisposePtr((Ptr) pathName);
  852.                 RegistryEntryIDDispose(&entry);
  853.             }
  854.         }
  855.         RegistryEntryIterateDispose(&cookie);
  856.     }
  857.     return;
  858. #endif
  859. }    /* GetVideoProperties */
  860.  
  861. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache)
  862. {
  863. #if NO_NAME_REGISTRY_LIB || !GENERATINGPOWERPC
  864.     cpuName[0]=0;
  865.     *cpuHz=0;
  866.     *hasL2Cache=0;
  867.     return;
  868. #else
  869.     /*
  870.     Assume we're running native on ppc.
  871.     If this were compiled as 68k code, the calls to the NameRegistryLib, which is PPC code, 
  872.     would require intervention by the Mixed Mode Manager,
  873.     and I decided not to bother with that complication, since there seems little point in
  874.     compiling programs as 68k code to run on a PowerPC. TimeVideo is distributed as a fat 
  875.     binary, so it always runs native.
  876.     */
  877.     OSStatus error;
  878.     RegEntryIter cookie;
  879.     Boolean done;
  880.     RegEntryIterationOp    op;
  881.     RegEntryID entry;
  882.     RegCStrPathName *pathName;
  883.     RegPathNameSize pathNameSize;
  884.     RegPropertyNameBuf foundProperty;
  885.     RegPropertyValueSize propertySize;
  886.     long templong;
  887.     char *propertyValue[256];
  888.     
  889.     cpuName[0]=0;
  890.     *cpuHz=0;
  891.     *hasL2Cache=0;
  892.     /*
  893.     {
  894.         // If the NameRegistryLib is weak-linked, and it is missing, we need to
  895.         // check for the library, to prevent a crash if we try to access the 
  896.         // library's exports;
  897.         // currently the Finder reports if the library is missing
  898.         CFragConnectionID        connID;
  899.         Ptr                        mainAddr;
  900.         Str255                    errName;
  901.         
  902.         error=Gestalt(gestaltCFMAttr,&templong);    // Code Fragment Manager?
  903.         if(!error)error=GetSharedLibrary((ConstStr63Param)"\pNameRegistryLib"
  904.             ,kAnyCFragArch,kFindCFrag,&connID,&mainAddr,errName);
  905.         if(error)return;    // No NameRegistryLib
  906.     }
  907.     */
  908.     error=Gestalt(gestaltNameRegistryVersion,&templong);
  909.     if(error)return;    // No PCI slots
  910.     // NameRegistryLib is weak-linked, and may be missing.
  911.     if((Ptr)RegistryEntryIterate == (Ptr)kUnresolvedCFragSymbolAddress)return;
  912.  
  913.     op=kRegIterContinue;
  914.     error=RegistryEntryIterateCreate(&cookie);
  915.     if(!error){
  916.         done=FALSE;
  917.         while (!error && !done){
  918.             error=RegistryEntryIterate(&cookie,op,&entry,&done);
  919.             if(!error && !done){
  920.                 pathName=NULL;
  921.                 error=RegistryEntryToPathSize(&entry,&pathNameSize);
  922.                 if(!error){
  923.                     pathName=(RegCStrPathName *) NewPtr(pathNameSize);
  924.                     if(pathName==NULL)error=MemError();
  925.                 }
  926.                 if(!error){
  927.                     error=RegistryCStrEntryToPath(&entry,pathName,pathNameSize);
  928.                     //printf("%s\n",pathName);    // debugging
  929.                 }
  930.                 if(!error){
  931.                     propertySize=0;
  932.                     strcpy(foundProperty,"device_type");
  933.                     error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  934.                     if(!error)assert(propertySize<=sizeof(propertyValue));
  935.                     if(!error){
  936.                         error=RegistryPropertyGet(&entry,foundProperty,propertyValue,&propertySize);
  937.                         // Is it what we want?
  938.                         if(!error && strcmp("cpu",(char *)propertyValue)!=0)error=-1; // no
  939.                     }
  940.                     if(!error){
  941.                         strcpy(foundProperty,"clock-frequency");
  942.                         error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  943.                     }
  944.                     if(!error)assert(propertySize==sizeof(*cpuHz));
  945.                     if(!error){
  946.                         error=RegistryPropertyGet(&entry,foundProperty,cpuHz,&propertySize);
  947.                     }
  948.                     if(!error){
  949.                         strcpy(foundProperty,"name");
  950.                         error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  951.                     }
  952.                     if(!error)assert(propertySize<256);
  953.                     if(!error){
  954.                         error=RegistryPropertyGet(&entry,foundProperty,cpuName,&propertySize);
  955.                     }
  956.                     if(!error){
  957.                         strcpy(foundProperty,"l2-cache");
  958.                         error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  959.                         if(!error)*hasL2Cache=1;
  960.                         else *hasL2Cache=0;
  961.                     }
  962.                     error=0;
  963.                 }
  964.                 if(pathName != NULL)DisposePtr((Ptr) pathName);
  965.                 RegistryEntryIDDispose(&entry);
  966.             }
  967.         }
  968.         RegistryEntryIterateDispose(&cookie);
  969.     }
  970.     return;
  971. #endif
  972. }    /* GetCPUProperties */
  973.